package com.datoucai.consumer;

import com.alibaba.fastjson.JSON;
import com.datoucai.dao.TbNotificationDao;
import com.datoucai.dto.CommentInfoDto;
import com.datoucai.dto.NotificationVO;
import com.datoucai.entity.TbNotification;
import com.datoucai.enums.NotificationEnumType;
import com.datoucai.param.MediaDetailInfoRes;
import com.datoucai.rpc.MediaRpcService;
import com.datoucai.utils.DateUtils;
import com.datoucai.utils.NotificationExtendUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Date;

@Component
@RocketMQMessageListener(
        topic = "ADD_COMMENT_TOPIC",
        consumerGroup = "notification-consumer-group"
)
@Slf4j
public class NotificationConsumer implements RocketMQListener<CommentInfoDto> {

    @Autowired
    private TbNotificationDao notificationDao;

    @Autowired
    private MediaRpcService mediaRpcService;
    @Autowired
    private PushNotificationService pushNotificationService;

    @Override
    public void onMessage(CommentInfoDto dto) {
        log.info("消息通知-消费入参:{}", JSON.toJSONString(dto));
        // 落库
        TbNotification tbNotification = new TbNotification();
        tbNotification.setType((byte)1);//通知类型（1-评论，2-回复，3-点赞等）
        tbNotification.setSenderId(dto.getUserId());
        tbNotification.setReceiverId(buildReceiverId(dto));
        tbNotification.setResourceId(dto.getResourceId());
        tbNotification.setCommentId(dto.getId());
        tbNotification.setContent("用户:"+dto.getUsername()+"评论了您的文章");
        tbNotification.setIsRead((byte)0);// 0未读 1已读
        tbNotification.setCreateTime(new Date());
        MediaDetailInfoRes mediaDetailInfoRes = mediaRpcService.queryMediaInfoById(dto.getResourceId()+"");
        tbNotification.setExtend(NotificationExtendUtils.buildExtend(mediaDetailInfoRes.getTitle(),buildTargetLink(mediaDetailInfoRes)));
        int count = notificationDao.insertSelective(tbNotification);
        log.info("消费消息-增加评论-消费状态:{}",count>0?"成功":"失败");
        // 通知前端
        NotificationVO vo = buildNotificationVO(tbNotification);
        pushNotificationService.pushNotification(tbNotification.getReceiverId(), vo);
    }

    private NotificationVO buildNotificationVO(TbNotification tbNotification) {
        NotificationVO vo = new NotificationVO();
        vo.setId(tbNotification.getId());
        vo.setType(NotificationEnumType.getByCode(tbNotification.getType()));
        vo.setContent(tbNotification.getContent());
        vo.setIsRead(tbNotification.getIsRead());
        vo.setCreateTime(DateUtils.date2Str(tbNotification.getCreateTime(),DateUtils.YYYY_MM_DD_HH_MM_SS));
        vo.setSenderId(tbNotification.getSenderId());
        vo.setSenderName(tbNotification.getContent());
        vo.setSenderAvatar(null);
        vo.setTargetId(tbNotification.getResourceId());
        MediaDetailInfoRes mediaDetailInfoRes = mediaRpcService.queryMediaInfoById(tbNotification.getResourceId()+"");

        vo.setTargetTitle(mediaDetailInfoRes.getTitle());
        vo.setTargetLink(buildTargetLink(mediaDetailInfoRes));
        return vo;
    }

    private String buildTargetLink(MediaDetailInfoRes mediaDetailInfoRes) {
        StringBuilder link = new StringBuilder();
        link.append("https://www.dtc996.com/front/blogDetail?blogId=").append(mediaDetailInfoRes.getId());
        return link.toString();
    }

    private Long buildReceiverId(CommentInfoDto dto) {
        Long resourceId = dto.getResourceId();
        MediaDetailInfoRes mediaDetailInfoRes = mediaRpcService.queryMediaInfoById(resourceId + "");
        Long userId = mediaDetailInfoRes!=null?mediaDetailInfoRes.getUserId(): null;
        Assert.isTrue(userId!=null,"接收者id不允许为空");
        return userId;
    }
}